package com.application.DataProvider;

import java.util.ArrayList;

import com.application.DTO.DetailTestQuestionDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DetailTestQuestionDAO{

	Context _mcontext = null;

	public DetailTestQuestionDAO(Context context){
		this._mcontext = context;
	}

	public boolean isExist(int id)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from DetailTestQuestion where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	public DetailTestQuestionDTO getDetailTestQuestion(int ID)
	{
		DetailTestQuestionDTO dtqDto = new DetailTestQuestionDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from DetailTestQuestion where ID = " + ID;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				dtqDto.set_ID(cur.getInt(0));
				if(cur.getInt(1) == 1)
					dtqDto.set_AnswerY_N(true);
				else
					dtqDto.set_AnswerY_N(false);
				dtqDto.set_IDTestQuestion(cur.getInt(2));
				dtqDto.set_idDetailTestQuestion(cur.getInt(3));
				dtqDto.set_Content(cur.getString(4));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return dtqDto;
	}
	public ArrayList<DetailTestQuestionDTO> getDetailTestQuestionByIDQuestion(int idTestQuestion)
	{
		ArrayList<DetailTestQuestionDTO> listDetailTestQuestion = new ArrayList<DetailTestQuestionDTO>();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from DetailTestQuestion where idTestQuestion = " + idTestQuestion;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				DetailTestQuestionDTO dtqDto = new DetailTestQuestionDTO();
				dtqDto.set_ID(cur.getInt(0));
				if(cur.getInt(1) == 1)
					dtqDto.set_AnswerY_N(true);
				else
					dtqDto.set_AnswerY_N(false);
				dtqDto.set_IDTestQuestion(cur.getInt(2));
				dtqDto.set_idDetailTestQuestion(cur.getInt(3));
				dtqDto.set_Content(cur.getString(4));
				listDetailTestQuestion.add(dtqDto);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return listDetailTestQuestion;
	}
	
	//Insert du lieu vao bang DetailLearning
	public int insertDetailLearning(DetailTestQuestionDTO dtqDto) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			//int id = getIDMax();
			values.put("ID", dtqDto.get_ID());
			values.put("AnswerYN", dtqDto.get_AnswerY_N());
			values.put("IDTestQuestion", dtqDto.get_IDTestQuestion());
			values.put("IDDetailTestQuestion", dtqDto.get_idDetailTestQuestion());
			values.put("Content", dtqDto.get_Content());
			
			result = provider.execInsertCommand(db, "DetailTestQuestion", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Cap nhat bang DetailLearning
	public int updateDetailLearning(DetailTestQuestionDTO dtqDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("ID", dtqDto.get_ID());
			values.put("AnswerYN", dtqDto.get_AnswerY_N());
			values.put("IDTestQuestion", dtqDto.get_IDTestQuestion());
			values.put("IDDetailTestQuestion", dtqDto.get_idDetailTestQuestion());
			values.put("Content", dtqDto.get_Content());
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(dtqDto.get_ID())};
			result= provider.execUpdateCommand(db, "DetailTestLearning", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}

		
	
	//Delete doi tuong DetailLearning
	public int deleteDetailTestQuestion(int id)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};	
			result=provider.execDeleteCommand(db, "DetailTestQuestion", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}

	
}
